package cn.yinzhiping.mplearn.controller;

import cn.yinzhiping.mplearn.domain.User;
import cn.yinzhiping.mplearn.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 测试使用mybatis-plus进行插入操作
     * @param user
     */
    @PostMapping("/testsave")
    public void testSave(@RequestBody User user){
        userService.save(user);
    }

    /**
     * 测试分页查询时，必须先配置分页插件
     * @return
     */
    @GetMapping("/testc")
    public IPage<User> testc(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.ge("dept_id","2");
        userService.list();
        IPage<User> page = new Page<>(1, 2);
        IPage<User> userIPage = userService.page(page,wrapper);

        System.out.println("总条数"+userIPage.getTotal());
        System.out.println("总页数"+userIPage.getPages());
        return  userService.page(page,wrapper);
    }
}
